package org.tp;

import java.awt.Dialog;
/** Classe concernant les prototypes**/
import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JOptionPane;
import javax.xml.ws.handler.MessageContext;
public class Prototypes {

	String forme;
	String type;
	String id;
	String lparam;

	Map<String, Prototypes> mapProto;

	int cpt_operator_proto;
	static int cpt_initial = 0;

	public Prototypes() {
		// TODO Auto-generated constructor stub
		mapProto = new HashMap<String, Prototypes>();
	}

	public Prototypes(String forme, String type, String id, String lparam, int cpt) {
		// TODO Auto-generated constructor stub
		this.forme = forme;
		this.type = type;
		this.id = id;
		this.lparam = lparam;

		if(forme.equals("operator")){
			this.cpt_operator_proto = cpt_initial;
			this.cpt_initial++;
		}
	}
	/** Ajout d'un prototype dans la map**/
	public void addMapProto(String forme, String type, String id, String lparam) {
		mapProto.put(type + " " + id +" "+lparam, new Prototypes(forme,type,id,lparam,cpt_operator_proto));
	}
	/** Accesseur pour obtenir un prototype suivant son identifiant**/
	public Prototypes getProto(String id) {
		return mapProto.get(id);
	}
	/** Accesseur pour obtenir la map**/
	public Map<String, Prototypes> getMapProto(){
		return mapProto;
	}
	/** Affichage d'un prototype**/
	public String afficherProto(){			
		if(this.forme.equals("procedure"))
			return new String("\tpublic abstract void "+id+"("+lparam+");\n");
		else if(this.forme.equals("operator")) {
			if(id.equals("<") || id.equals(">") || id.equals("<=") || id.equals(">=") || id.equals("!=") || id.equals("==")){
				//cpt_operator_proto++;
				return new String("\tpublic abstract boolean operator"+cpt_operator_proto+" ("+lparam+");\n");
			}
			else {
				//cpt_operator_proto++;
				return new String("\tpublic abstract double operator"+cpt_operator_proto+" ("+lparam+");\n");
			}
		}
		else 
			return new String("\tpublic abstract "+type+" "+id+"("+lparam+");\n");
	}
	/** Affichage de tous les prototypes presents dans notre map**/
	public String afficherAllProto() {
		String s1 =  new String("public interface MyInterface {\n");
		String tmp = "";//operator
		String tmp2 = "";//les autres
		for(Entry<String, Prototypes> entry: getMapProto().entrySet())
			if(entry.getValue().forme.equals("operator"))
				tmp = tmp.concat(entry.getValue().afficherProto());
			else 
				tmp2 = tmp2.concat(entry.getValue().afficherProto());
		s1 = s1.concat(tmp).concat(tmp2);
		String s2 = new String("}\n");
		return s1.concat(s2);
	}

	public String afficherOP_Func(){			
		return this.afficherProto().replace("public abstract","").replace("\t","").replace(";\n","");
	}
	/** controle que tout les prototypes sont implementes par une fonction**/
	public void controle_fct(){
		boolean exist = false;
		Map<String, Fonctions> mapFunc = Main.func.getMapFunc(); 
		for(Entry<String, Prototypes> entry: getMapProto().entrySet()){
			exist = false;

			String tmp = entry.getValue().afficherProto().replace("\tpublic abstract ","null  ").replace(";\n", " null");
			/*if(tmp.contains("operator"))
				System.out.println(tmp);
			else
				System.out.println(entry.getKey());
			System.out.println("-------------------");*/

			for(Entry<String, Fonctions> entry2: mapFunc.entrySet()){
				//System.out.println(entry2.getKey());
				if(tmp.contains("operator")){
					if(entry2.getKey().equals(tmp) && !entry2.getKey().contains("static") ){
						//System.out.println(entry.getValue().afficherProto().replace("\tpublic abstract ","null ").replace(";", " null")+"\n"+entry2.getKey()+"\n\n");
						exist = true;
					}
				}
				else {
					if(entry2.getKey().equals(entry.getKey()) && !entry2.getKey().contains("static") ){
						exist = true;
					}
				}
			}
			//System.out.println();
			if(!exist)
			{JOptionPane.showMessageDialog(null, "erreur : le prototype : " + entry.getValue().afficherProto() + " n'existe pas");}
		}
	}
}
